﻿using System;
using System.Reflection;
using System.IO;

namespace BizElements.Generator
{
    sealed class ProjectController
    {
        #region Project.

        public IProjectUserControl ProjectUserControl { get; set; }
        public string ProjectFile { get; set; }
        public BizElementsSettings ProjectSettings { get; set; }        

        public void InitializeControl(IProjectUserControl ucProj)
        {
            this.ProjectUserControl = ucProj;
            ucProj.Visible = true;
            ReadProjectSettings();
        }

        public void ReadProjectSettings()
        {
            this.ProjectUserControl.ReadProjectSettings(this.ProjectSettings);
        }

        public void WriteProjectSettings()
        {
            if (this.ProjectUserControl != null && this.ProjectSettings != null)
            {
                this.ProjectSettings.Clear();
                this.ProjectUserControl.WriteProjectSettings(this.ProjectSettings);
            }
        }

        public static BizElementsSettings NewOrmProject()
        {
            string ormFile = IoUtil.ToAbsFilePath(Properties.Settings.Default.ProjectController_NewOrmProjectFile);
            if ((ormFile != null) && File.Exists(ormFile))
                return BizElementsSettings.LoadFromFile(ormFile);
            else
                return new BizElementsSettings(BizElementsSettingsType.OrmProject);
        }

        public static BizElementsSettings NewDotNetProject()
        {
            string netFile = IoUtil.ToAbsFilePath(Properties.Settings.Default.ProjectController_NewDotNetProjectFile);
            if ((netFile != null) && File.Exists(netFile))
                return BizElementsSettings.LoadFromFile(netFile);
            else
                return new BizElementsSettings(BizElementsSettingsType.DotNetProject);
        }

        public static BizElementsSettings NewAspNetProject()
        {
            string netFile = IoUtil.ToAbsFilePath(Properties.Settings.Default.ProjectController_NewAspNetProjectFile);
            if ((netFile != null) && File.Exists(netFile))
                return BizElementsSettings.LoadFromFile(netFile);
            else
                return new BizElementsSettings(BizElementsSettingsType.DotNetProject);
        }

        public static BizElementsSettings NewOrmAndBlProject()
        {
            string ormFile = IoUtil.ToAbsFilePath(Properties.Settings.Default.ProjectController_NewOrmAndBlProjectFile);
            if ((ormFile != null) && File.Exists(ormFile))
                return BizElementsSettings.LoadFromFile(ormFile);
            else
                return new BizElementsSettings(BizElementsSettingsType.OrmProject);
        }

        public static BizElementsSettings NewOrmMinimumProject()
        {
            string ormFile = IoUtil.ToAbsFilePath(Properties.Settings.Default.ProjectController_NewOrmMinimumProjectFile);
            if ((ormFile != null) && File.Exists(ormFile))
                return BizElementsSettings.LoadFromFile(ormFile);
            else
                return new BizElementsSettings(BizElementsSettingsType.OrmProject);
        }

        #endregion

        #region Title bar.

        const string BuildType = "";

        public string GetTitleBarText()
        {
            Version ver = Assembly.GetExecutingAssembly().GetName().Version;
            string currentProject = (this.ProjectFile != null) ? this.ProjectFile : Messages.ProjectController_Untitled;
            int filenameStartIdx = currentProject.LastIndexOfAny(new char[] { '\\', '/' }) + 1;
            if (filenameStartIdx > 0 && filenameStartIdx < currentProject.Length)
                currentProject = currentProject.Substring(filenameStartIdx);

            string projType = GetProjectTypeName();
            if (!string.IsNullOrEmpty(projType))
                projType = " - " + projType;
            return string.Format(Messages.DbProjectFrm_TitleBarFormat, currentProject, ver.Major, ver.Minor, ver.Build, ver.Revision, BuildType, projType);
        }

        private string GetProjectTypeName()
        {
            if (this.ProjectSettings == null)
                return "";

            switch (this.ProjectSettings.SettingsType)
            { 
                case BizElementsSettingsType.OrmProject:
                    return Messages.ProjectController_OrmProjectTitle;
                case BizElementsSettingsType.DotNetProject:
                    return Messages.ProjectController_DotNetProjectTitle;
                default:
                    return "";
            }
        }

        #endregion
    }    
}
